package com.jeomo.base.starter.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @Author: qbt
 * @Date: 2019/4/10 17:54
 * @Version 1.0
 */
@EnableTransactionManagement
@Configuration
@MapperScan("com.jeomo.**.mapper*")
public class MybatisPlusConfig {

    /**
     * 使用多个功能需要注意顺序关系,建议使用如下顺序
     *
     * 多租户,动态表名
     * 分页,乐观锁
     * sql性能规范,防止全表更新与删除
     * 总结: 对sql进行单次改造的优先放入,不对sql进行改造的最后放入
     * 分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() { // 多租户
//            @Override
//            public Expression getTenantId() {
//                return new StringValue(LoginSessionUtils.getCurrentLoginInfo().getTopOrgCode());
//            }
//
//            @Override
//            public String getTenantIdColumn() {
//                return "TOP_ORG_CODE";
//            }
//
//            @Override
//            public boolean ignoreTable(String tableName) {
//                if("sys_dict".equals(tableName)) {
//                    return true;
//                }
//                return false;
//            }
//        }));
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 配置分页插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 配置乐观锁
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }

    /**
     * 配置乐观锁
     */






}
